package de.hs_zigr.inf.iib08gruppe5powerfish.it.boundary;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JOptionPane;



/**
 * for recognizing the selected item in a list of queries
 */
public class MyMouseListener implements MouseListener {

	private GUIQueryProg guiQueryProg;
	private GUIListOfQueries guiListOfQueries;

	public MyMouseListener(GUIQueryProg guiQueryProg,
			GUIListOfQueries guiListOfQueries) {
		this.guiQueryProg = guiQueryProg;
		this.guiListOfQueries = guiListOfQueries;
	}

	/** recognize selected item */
	public void mouseClicked(MouseEvent me) {
		Point p = new Point(me.getX(), me.getY());
		guiListOfQueries.getLstQueries().setSelectedIndex(
				guiListOfQueries.getLstQueries().locationToIndex(p));
		guiQueryProg.setBMayShow(true);
		guiQueryProg.runScript();
		try {
			guiQueryProg.getCtrlQueryProg().storeQuery2FileHistory(
					guiQueryProg.getTexttAreaInput());
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			JOptionPane.showMessageDialog(guiListOfQueries.getFrmQueryLst(),
					"Fatal Exception (QueryStorer). Please contact support. ",
					"program error", JOptionPane.ERROR_MESSAGE);
		}
		guiListOfQueries.getFrmQueryLst().dispatchEvent(
				new WindowEvent(guiListOfQueries.getFrmQueryLst(),
						WindowEvent.WINDOW_CLOSING));
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}